package cn.jianyongjie.org.service.impl;

import cn.jianyongjie.basic.service.impl.BaseServiceImpl;
import cn.jianyongjie.org.domain.Department;
import cn.jianyongjie.org.mapper.DepartmentMapper;
import cn.jianyongjie.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @Description: service层的实现类
* @Author: Jian Yongjie
* @Version: 1.0
* @Date:  2022/4/19 18:46
*/
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    /*
    * @Title: DepartmentServiceimpl
    * @Description: 新增数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/19 18:45
    * @Parameters: * @param department
    * @Return void
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath = "";
        // 判断是否是顶级对象
        if(department.getParent()==null){
            // 如果是顶级对象就拼接自己的id
            dirPath = "/"+ department.getId();

        }else {
            // 根据自己的父亲id查询上一级数据
            Department departmentById = departmentMapper.LoadById(department.getParent().getId());
            // 因为父类的dirPath自己就拥有了上级所有路径，所以只需要把自己拼接上去就行了
            if (departmentById!=null){
                dirPath = departmentById.getDirPath()+"/"+department.getId();
                System.out.println(departmentById.getDirPath());
            }
        }

        // 将拼接的数据设置进去
        department.setDirPath(dirPath);
        // 调用修改方法进行修改
        departmentMapper.update(department);
    }

    /*
    * @Title: DepartmentServiceimpl
    * @Description: 根据id修改数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/19 18:45
    * @Parameters: * @param department
    * @Return void
    */
    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        // 判断是否是顶级对象
        if(department.getParent()==null){
            // 如果是顶级对象就拼接自己的id
            dirPath = "/"+ department.getId();

        }else {
            // 根据自己的父亲id查询上一级数据
            Department departmentById = departmentMapper.LoadById(department.getParent().getId());
            // 因为父类的dirPath自己就拥有了上级所有路径，所以只需要把自己拼接上去就行了
            if (departmentById!=null){
                dirPath = departmentById.getDirPath()+"/"+department.getId();
                System.out.println(departmentById.getDirPath());
            }
        }

        // 将拼接的数据设置进去
        department.setDirPath(dirPath);
        // 调用修改方法进行修改
        departmentMapper.update(department);
    }



    /*
    * @Title: DepartmentServiceimpl
    * @Description: 查询部门树数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/24 11:56
    * @Parameters: * @param
    * @Return java.util.List<cn.jianyongjie.org.domain.Department>
    */
    @Override
    public List<Department> findTree() {
        // 查询所有数据
        List<Department> dept = departmentMapper.loadAll();
        // 创建map集合
        HashMap<Long, Department> deptMap = new HashMap<>();
        // 将list集合转为map集合
        for (Department department : dept) {
            deptMap.put(department.getId(),department);
        }
        // 创建一个空集合
        List<Department> depts = new ArrayList<>();
        // 循环查询到的所有数据
        for (Department department : dept) {
            if (department.getParent_id()==null){
                // 将查询到的一级部门放进新的集合里面
                depts.add(department);
            }else {
                // 根据parent_id，获取到上一级部门
                Department department1 = deptMap.get(department.getParent_id());
                // 判断父级对象是否为空
                if(department1!=null){
                // 如果不为空就将数据装到children字段里面
                    department1.getChildren().add(department);
                }

            }
        }
        return depts;
    }

}
